package com.jdjw.modules.thirdpost.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdjw.common.ResponseResult;
import com.jdjw.modules.buscomplaint.entity.BusComplaint;
import com.jdjw.modules.buscomplaint.mapper.BusComplaintMapper;
import com.jdjw.modules.thirdpost.param.BusComplaintAcceptParam;
import com.jdjw.modules.thirdpost.param.BusComplaintParam;
import com.jdjw.modules.thirdpost.param.ThirdPostInParm;
import com.jdjw.utils.CommonUtil;
import com.jdjw.utils.ConstantUtil;
import com.jdjw.utils.DateUtil;
import com.jdjw.utils.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 群众投诉表 服务实现类
 * </p>
 *
 * @author fq
 * @since 2021-06-10
 */
@Service
@Slf4j
public class BusComplainttwoServiceImpl extends ServiceImpl<BusComplaintMapper, BusComplaint> implements IService<BusComplaint> {


    @Autowired
    static BusComplaintMapper busComplaintMapper;

    @Autowired
    static ThirdPostExceptionServiceImpl thirdPostExceptionServiceImpl;

    @Resource
    public void setThirdPostExceptionServiceImpl(ThirdPostExceptionServiceImpl thirdPostExceptionServiceImpl) {
        BusComplainttwoServiceImpl.thirdPostExceptionServiceImpl = thirdPostExceptionServiceImpl;
    }

    @Resource
    public void setBusComplaintMapper(BusComplaintMapper busComplaintMapper) {
        BusComplainttwoServiceImpl.busComplaintMapper = busComplaintMapper;
    }


    public static void doPostBusProcureApplyZi(ThirdPostInParm thirdPostInParm) {
        String url = "";
        //全量
        if (ConstantUtil.ALL_TYPE.equals(thirdPostInParm.getMethodType())) {
            url = ConstantUtil.ALL_THIRD_POST + 28;
        } else if (ConstantUtil.ADD_TYPE.equals(thirdPostInParm.getMethodType())) {
            //增量必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.ADD_THIRD_POST + 28;
        } else {
            //更新必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.UPDATE_THIRD_POST + 28;
        }
        try {
            //查询
            List<BusComplaint> list = busComplaintMapper.selectListCha(thirdPostInParm);
            if (list == null || list.size() < 1) {
                log.info("群众诉求-基本信息表数据库查询数据为0条");
                return;
            }
            if (list.size() > 3000) {
                Integer count = list.size() / 3000 + 1;
                Collection<Collection<BusComplaint>> fgList = CommonUtil.subCollection(list, count);
                for (Collection<BusComplaint> obj : fgList) {
                    sendData((List<BusComplaint>) obj, url);
                }
            } else {
                sendData(list, url);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static void sendData(List<BusComplaint> obj, String url) {
        ResponseResult result = new ResponseResult();
        ArrayList list = new ArrayList();
        for (BusComplaint a : obj) {
            BusComplaintParam param = new BusComplaintParam();
            param.setBiz_id(a.getId());
            param.setId(a.getId());
            param.setIs_xzjd(2);//2
//            param.setSq_xzjdxzqh();//null? 都是村级的，没有镇级的
            param.setIs_cwh(1);//1
            param.setSq_cwhxzqh(a.getDeptId());
            param.setSqly(2);//2
            param.setSqlx(suqiuleixing(a.getEventType()));
            param.setSqr(a.getComplainantName());
            param.setSfnm(shiFou(a.getIfAnonymous()));
            param.setLxfs(a.getComplainantContactWay());
            param.setSqnr(a.getComplaintContent());//我们的太长
            param.setTjsj(BusProcureApplyServiceImpl.stringDate(a.getComplaintTime()));
            param.setSfsj(2);//2
            param.setSfsl(Integer.parseInt(a.getSfsl()));
//            param.setSljjjcjgcj();//discipline_department 没有数据
//            param.setSljjjcjgmc();
//            param.setJjjcslsj();
//            param.setJjjcslsc();
            param.setSffszspt(Integer.parseInt(a.getSffszspt()));
            param.setQdsqlx(suqiuleixing(a.getEventType()));
            param.setSldw(a.getAcceptDepartment());
            param.setSlsj(BusProcureApplyServiceImpl.stringDate(a.getAcceptorTime()));
            param.setSlr(a.getAcceptorName());
//            param.setBlzt(Integer.parseInt(a.getTransactStage()));//数据库是空的不知道存储的是中文还是编码。
            param.setBjsc(a.getBjsc());
//            param.setHfsj();//null
            param.setSqblpj(suQiu(a.getCommentGrade()));
            param.setSqblpjnr(a.getCommentText());
//            param.setSffqdb(a.getSuperviseStatus());//数据库是空的不知道存储的是中文还是编码。
//            param.setFqdbdw();//null
            param.setFqdbsj(BusProcureApplyServiceImpl.stringDate(a.getSupervisetime()));
            param.setDel_flag(0);
            param.setCwh(a.getCwh());
            param.setCwhxzqh(CommonUtil.addZeroForNum(a.getDeptId(), 16));

            //转化为json字符串
            String jsonStr = JSONObject.toJSONString(param);
            //转化为json对象
            list.add(JSONObject.parseObject(jsonStr));
        }
        //发送(post)
        net.sf.json.JSONObject jsonObject = HttpRequestUtil.getHttpResponseJsonAllSSL(url, list);
        System.out.println("结果："+jsonObject);
        if (jsonObject != null) {
            System.out.println("返回: " + jsonObject);
            if ("200".equals(jsonObject.get("code"))) {
                result.setCode(200);
                result.setMsg("SUCCESS");
                result.setData("推送完成");
                log.info("群众诉求-基本信息表推送成功"+list.size(), result);
            } else {
                //记录日志
                thirdPostExceptionServiceImpl.insertExceptionData(
                        "doPostBusProcureApplyZi", "群众诉求-基本信息表数据",
                        jsonObject.getString("message"),
                        DateUtil.formatDate(DateUtil.FORMAT_YMD, new Date().getTime()));
                result.setCode(500);
                result.setMsg("ERROR");
                result.setData(jsonObject.getString("message"));
                log.info("群众诉求-基本信息表推送失败", result);
            }
        } else {
            log.info("群众诉求-基本信息表推送失败", "第三方返回空");
        }
    }


    /**
     * 附件信息
     */
    public static void doPostBusProcureErJiu(ThirdPostInParm thirdPostInParm) {
        String url = "";
        //全量
        if (ConstantUtil.ALL_TYPE.equals(thirdPostInParm.getMethodType())) {
            url = ConstantUtil.ALL_THIRD_POST + 29;
        } else if (ConstantUtil.ADD_TYPE.equals(thirdPostInParm.getMethodType())) {
            //增量必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.ADD_THIRD_POST + 29;
        } else {
            //更新必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.UPDATE_THIRD_POST + 29;
        }
        try {
            //查询
            List<BusComplaint> list = busComplaintMapper.selectListFu(thirdPostInParm);
            if (list == null || list.size() < 1) {
                log.info("群众诉求-附件信息表数据库查询数据为0条");
                return;
            }
            if (list.size() > 3000) {
                Integer count = list.size() / 3000 + 1;
                Collection<Collection<BusComplaint>> fgList = CommonUtil.subCollection(list, count);
                for (Collection<BusComplaint> obj : fgList) {
                    sendDatatwo((List<BusComplaint>) obj, url);
                }
            } else {
                sendDatatwo(list, url);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 附件信息数据封装
     *
     * @param obj
     * @param url
     */
    private static void sendDatatwo(List<BusComplaint> obj, String url) {
        ResponseResult result = new ResponseResult();
        ArrayList list = new ArrayList();
        int b = 0;
        for (BusComplaint a : obj) {
            BusComplaintAcceptParam param = new BusComplaintAcceptParam();
            param.setId(a.getId());
            param.setBiz_id(a.getBizId());
            param.setParent_id(a.getId());
            param.setFjlj(a.getFileId());
            param.setDel_flag(0);
            param.setCwh(a.getCwh());
            param.setCwhxzqh(CommonUtil.addZeroForNum(a.getDeptId(), 16));

            //转化为json字符串
            String jsonStr = JSONObject.toJSONString(param);
            //转化为json对象
            list.add(JSONObject.parseObject(jsonStr));
        }
        //发送(post)
        net.sf.json.JSONObject jsonObject = HttpRequestUtil.getHttpResponseJsonAllSSL(url, list);
        System.out.println("结果："+jsonObject);
        if (jsonObject != null) {
            if ("200".equals(jsonObject.get("code"))) {
                result.setCode(200);
                result.setMsg("SUCCESS");
                result.setData("推送完成");
                log.info("群众诉求-附件信息表数据推送成功"+list.size(), result);
            } else {
                //记录日志
                thirdPostExceptionServiceImpl.insertExceptionData(
                        "doPostBusProcureErJiu", "群众诉求-附件信息表数据",
                        jsonObject.getString("message"),
                        DateUtil.formatDate(DateUtil.FORMAT_YMD, new Date().getTime()));
                result.setCode(500);
                result.setMsg("ERROR");
                result.setData(jsonObject.getString("message"));
                result.setData(JSON.toJSONString(jsonObject));
                log.info("群众诉求-附件信息表推送失败", result);
            }
        } else {
            log.info("群众诉求-附件信息表推送失败", "第三方返回空");
        }
    }


    /**
     * 办理信息
     */
    public static void doPostBusProcureBanLi(ThirdPostInParm thirdPostInParm) {
        String url = "";
        //全量
        if (ConstantUtil.ALL_TYPE.equals(thirdPostInParm.getMethodType())) {
            url = ConstantUtil.ALL_THIRD_POST + 22;
        } else if (ConstantUtil.ADD_TYPE.equals(thirdPostInParm.getMethodType())) {
            //增量必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.ADD_THIRD_POST + 22;
        } else {
            //更新必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.UPDATE_THIRD_POST + 22;
        }
        try {
            //查询
            List<BusComplaint> list = busComplaintMapper.selectListBan(thirdPostInParm);
            if (list == null || list.size() < 1) {
                log.info("群众诉求-办理信息表数据库查询数据为0条");
                return;
            }
            if (list.size() > 3000) {
                Integer count = list.size() / 3000 + 1;
                Collection<Collection<BusComplaint>> fgList = CommonUtil.subCollection(list, count);
                for (Collection<BusComplaint> obj : fgList) {
                    sendDataBanLi((List<BusComplaint>) obj, url);
                }
            } else {
                sendDataBanLi(list, url);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 附件信息数据封装
     *
     * @param obj
     * @param url
     */
    private static void sendDataBanLi(List<BusComplaint> obj, String url) {
        ResponseResult result = new ResponseResult();
        ArrayList list = new ArrayList();
        for (BusComplaint a : obj) {
            BusComplaintAcceptParam param = new BusComplaintAcceptParam();
            param.setId(a.getId());
            param.setBiz_id(a.getBizId());
            param.setParent_id(a.getDeptId());
            param.setDel_flag(0);
            param.setCwh(a.getCwh());
            param.setCwhxzqh(CommonUtil.addZeroForNum(a.getDeptId(), 16));
            if ("1".equals(a.getSfsl())) {
                param.setSfjs(0);
            } else if ("2".equals(a.getSfsl())) {
                param.setSfjs(1);
            }
            param.setJssj(BusProcureApplyServiceImpl.stringDate(a.getAcceptorTime()));
            param.setBldw(a.getTransactStage());
            param.setYjblqx(BusProcureApplyServiceImpl.stringDate(a.getVideo()));
            param.setSfwcbl(2);//传输的都是已完成的
            param.setBlwcsj(BusProcureApplyServiceImpl.stringDate(a.getCompletiontime()));
            param.setBlfqxx(a.getCompletion());
            param.setSfgk(2);//不公开
            param.setSfbj(2);//只传输已签收已办结的数据，所以都是办结的:2
            param.setSfjsdbhcb(2);

            //转化为json字符串
            String jsonStr = JSONObject.toJSONString(param);
            //转化为json对象
            list.add(JSONObject.parseObject(jsonStr));
        }
        //发送(post)
        net.sf.json.JSONObject jsonObject = HttpRequestUtil.getHttpResponseJsonAllSSL(url, list);
        System.out.println("结果："+jsonObject);
        if (jsonObject != null) {
            if ("200".equals(jsonObject.get("code"))) {
                result.setCode(200);
                result.setMsg("SUCCESS");
                result.setData("推送完成");
                log.info("群众诉求-办理信息数据推送成功"+list.size(), result);
            } else {
                //记录日志
                thirdPostExceptionServiceImpl.insertExceptionData(
                        "doPostBusProcureBanLi", "群众诉求-办理信息数据",
                        jsonObject.getString("message"),
                        DateUtil.formatDate(DateUtil.FORMAT_YMD, new Date().getTime()));
                result.setCode(500);
                result.setMsg("ERROR");
                result.setData(jsonObject.getString("message"));
                log.info("群众诉求-办理信息表推送失败", result);
            }
        } else {
            log.info("群众诉求-办理信息表推送失败", "第三方返回空");
        }
    }


    /**
     * 诉求类型
     */
    public static int suqiuleixing(String eventType) {
        if ("信访维稳".equals(eventType)) {
            return 1;
        } else if ("矛盾纠纷".equals(eventType)) {
            return 2;
        } else if ("治安隐患".equals(eventType)) {
            return 3;
        } else if ("食药安全".equals(eventType)) {
            return 4;
        } else if ("工商监管".equals(eventType)) {
            return 5;
        } else if ("质量监管".equals(eventType)) {
            return 6;
        } else if ("金融监管".equals(eventType)) {
            return 7;
        } else if ("城市管理".equals(eventType)) {
            return 8;
        } else if ("交通安全".equals(eventType)) {
            return 9;
        } else if ("安全生产".equals(eventType)) {
            return 10;
        } else if ("消防安全".equals(eventType)) {
            return 11;
        } else if ("治危拆违".equals(eventType)) {
            return 12;
        } else if ("生态环境".equals(eventType)) {
            return 13;
        } else if ("农林水利".equals(eventType)) {
            return 14;
        } else if ("自然灾害".equals(eventType)) {
            return 15;
        } else if ("扫黄打非".equals(eventType)) {
            return 16;
        } else if ("困难救助".equals(eventType)) {
            return 17;
        } else if ("优抚安置".equals(eventType)) {
            return 18;
        } else if ("文教体育".equals(eventType)) {
            return 19;
        } else if ("卫生健康".equals(eventType)) {
            return 20;
        } else if ("SJ0421330000021 便民服务 老龄殡葬".equals(eventType)) {
            return 21;
        } else {
            return 22;
        }
    }

    /**
     * 1：是2：否
     */
    public static int shiFou(String shuJu) {
        if ("是".equals(shuJu)) {
            return 1;
        } else if ("否".equals(shuJu)) {
            return 2;
        }
        return 1;
    }

    /**
     * 诉求办理评价
     */
    public static int suQiu(String suQiuStr) {
        if ("不满意".equals(suQiuStr)) {
            return 1;
        } else if ("不太满意".equals(suQiuStr)) {
            return 2;
        } else if ("一般".equals(suQiuStr)) {
            return 3;
        } else if ("比较满意".equals(suQiuStr)) {
            return 4;
        } else if ("非常满意".equals(suQiuStr)) {
            return 5;
        }
        return 5;
    }

    //UUId生成
    private static String getUuid() {

        return UUID.randomUUID().toString().replaceAll("-", "");
    }

}
